package leetCode;

import java.util.ArrayList;
import java.util.List;

public class Q144 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {}

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public List<Integer> res = new ArrayList<>();

    public List<Integer> preorderTraversal(TreeNode root) {
        if(root!=null){
            x(root);
        }

        return this.res;
    }

    public void x(TreeNode root){
        this.res.add(root.val);

        if(root.left!=null){
            x(root.left);
        }
        if(root.right!=null){
            x(root.right);
        }
    }
}
